#include "device/qmc5883/qmc5883_rt1064.h"



void QMC5883P_RT1064::delay_ms(uint32_t _ms)
{
    //HAL_Delay(_ms);
		uint16_t sleep_time = 20000;
		while(_ms--)
		{
			while(sleep_time--);
		}
}

unsigned char QMC5883P_RT1064::i2c_write(unsigned char slave_addr,
                                    unsigned char reg_addr,
                                    unsigned short len,
                                    unsigned char *data_ptr)

{
		status_t reVal= kStatus_Fail;
		size_t txCount        = 0xFFU;
		  if (kStatus_Success == LPI2C_MasterStart(base, slave_addr, kLPI2C_Write))
    {
        /* Check master tx FIFO empty or not */
        LPI2C_MasterGetFifoCounts(base, NULL, &txCount);
        while (txCount)
        {
            LPI2C_MasterGetFifoCounts(base, NULL, &txCount);
        }
        /* Check communicate with slave successful or not */
        if (LPI2C_MasterGetStatusFlags(base) & kLPI2C_MasterNackDetectFlag)
        {
            return kStatus_LPI2C_Nak;
        }

        /* subAddress = 0x01, data = g_master_txBuff - write to slave.
          start + slaveaddress(w) + subAddress + length of data buffer + data buffer + stop*/
        reVal = LPI2C_MasterSend(base, &reg_addr, 1);
        if (reVal != kStatus_Success)
        {
            if (reVal == kStatus_LPI2C_Nak)
            {
                LPI2C_MasterStop(base);
            }
            return -1;
        }

        reVal = LPI2C_MasterSend(base, data_ptr, len);
        if (reVal != kStatus_Success)
        {
            if (reVal == kStatus_LPI2C_Nak)
            {
                LPI2C_MasterStop(base);
            }
            return -1;
        }

        reVal = LPI2C_MasterStop(base);
        if (reVal != kStatus_Success)
        {
            return -1;
        }
    }
   return 0;
}

unsigned char QMC5883P_RT1064::i2c_read(unsigned char Address,
                                      unsigned char RegisterAddr,
                                      unsigned int RegisterLen,
                                      unsigned char *RegisterValue)
{
     status_t reVal= kStatus_Fail;
		size_t txCount        = 0xFFU;
	/* Receive blocking data from slave */
    /* subAddress = 0x01, data = g_master_rxBuff - read from slave.
      start + slaveaddress(w) + subAddress + repeated start + slaveaddress(r) + rx data buffer + stop */
    if (kStatus_Success == LPI2C_MasterStart(base, Address, kLPI2C_Write))
    {
        /* Check master tx FIFO empty or not */
        LPI2C_MasterGetFifoCounts(base, NULL, &txCount);
        while (txCount)
        {
            LPI2C_MasterGetFifoCounts(base, NULL, &txCount);
        }
        /* Check communicate with slave successful or not */
        if (LPI2C_MasterGetStatusFlags(base) & kLPI2C_MasterNackDetectFlag)
        {
            return kStatus_LPI2C_Nak;
        }

        reVal = LPI2C_MasterSend(base, &RegisterAddr, 1);
        if (reVal != kStatus_Success)
        {
            if (reVal == kStatus_LPI2C_Nak)
            {
                LPI2C_MasterStop(base);
            }
            return -1;
        }

        reVal = LPI2C_MasterRepeatedStart(base, Address, kLPI2C_Read);
        if (reVal != kStatus_Success)
        {
            return -1;
        }

        reVal = LPI2C_MasterReceive(base, RegisterValue, RegisterLen);
        if (reVal != kStatus_Success)
        {
            if (reVal == kStatus_LPI2C_Nak)
            {
                LPI2C_MasterStop(base);
            }
            return -1;
        }

        reVal = LPI2C_MasterStop(base);
        if (reVal != kStatus_Success)
        {
            return -1;
        }
    }
	return 0;
}

